<?php

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

namespace app\cron\controller;

use think\Db;
use think\Cache;

/**
 * Description of Copyddbbotoindex
 *
 * @author dxx
 */
class Copyddbbotoindex extends \think\Controller {

    public function __construct(\think\Request $request = null) {
        parent::__construct($request);
    }

    public function run() {
        while (1) {
            echo "\n" . 'start' . "\n";
            Cache::select(6);
            $startACRMNO = Cache::get('Copyddbbotoindex');

            if (!$startACRMNO) {
                $startACRMNO = 'BD00000000';
            }
            echo $startACRMNO . "\n";

            $where['member.ACRMNO'] = ['gt', $startACRMNO];
            $where['member.MobileNO'] = ['neq', ''];

            $bdmemberList = Db::table('ddbbo.bdmember')->alias('member')
                            ->join('ddbbo.bdmemberinfo info', 'member.ACRMNO = info.ACRMNO', 'left')
                            ->order('member.ACRMNO asc')->where($where)->limit(100)->column('member.ACRMNO cardno,member.MobileNO mobile,member.BOD birthday,member.Name name,REPLACE(REPLACE(member.Gender,\'M\',\'1\'),\'F\',\'2\') gender,info.Info1 account_code');

            foreach ($bdmemberList as $value) {
                try {
                    Db::table('jooj_member.jj_member_index_1')->startTrans();
                    $data = [
                        'come_from' => 10,
                        'mp_id' => 2,
                        'account_code' => $value['account_code'],
                        'name' => $value['name'],
                        'gender' => $value['gender'],
                        'birthday' => $value['birthday'],
                        'mobile' => $value['mobile'],
                        'mobile_status' => 1,
                        'cardno' => $value['cardno'],
                        'card_lev' => 1,
                        'add_ts' => time(),
                        'last_ts' => time(),
                    ];
                    $member_code = Db::table('jooj_member.jj_member_index_1')->insertGetId($data);

                    Db::table('jooj_member.jj_member_index_info_1')->insert(['member_code' => $member_code]);

                    if ($member_code) {
                        $data = [
                            'member_code' => $member_code,
                            'name' => $value['name'],
                            'gender' => $value['gender'],
                            'birthday' => $value['birthday'],
                            'mobile' => $value['mobile'],
                            'cardno' => $value['cardno'],
                            'card_lev' => 1,
                            'add_ts' => time(),
                            'last_ts' => time(),
                        ];
                        $vip_id = Db::table('jooj_member.jj_member_vip_1')->insertGetId($data);

                        $member_code = Db::table('jooj_member.jj_member_index_1')->where(['member_code' => $member_code])->update(['vip_id' => $vip_id]);
                        Db::table('jooj_member.jj_member_index_1')->commit();

                        Cache::select(6);
                        Cache::set('Copyddbbotoindex', $value['cardno']);
                    }
                } catch (\Exception $e) {
                    echo 'Exception:' . $value['cardno'] . "\n";
                    echo $e->getMessage();
                    Db::table('jooj_member.jj_member_index_1')->rollback();

                    Cache::select(6);
                    Cache::set('Copyddbbotoindex', $value['cardno']);

                    continue;
                }
            }
        }
    }

}
